<?php

class Main extends MY_Controller {

    public function __construct() {
        parent::__construct();

        //Set layout and view folder to be used
        $this->layouts->set_layout('home');
        $this->layouts->set_view_folder('main');

        //Load CSS and JS (jqueryUI Dialog)
        //Load libraries
        //Load models
        $this->load->model('main_model');

        //$this->output->enable_profiler(TRUE);
    }

    function index() {


        $this->layouts->set_js('flowchart');

        $data['test'] = "yahoo";

        //$this->layouts->view('general_info', $data);
        $this->layouts->view('test', $data); //-->Flowchart: put later
    }

    function register() {
        
    }

    function do_register() {
        
    }

    function login() {

        //TODO: Validation for login field; Redo show errors for login
        //Perform login check
        $email = $this->input->post('email');
        $password = SHA1($this->input->post('password'));

        $logged_in = $this->main_model->do_login($email, $password);

        if ($logged_in->num_rows() == 0) {
            $this->layouts->view('login_error');
        } else {
            foreach ($logged_in->result() as $row) {
                $this->session->set_userdata('name', $row->first_name);
                $this->session->set_userdata('user_id', 1); //dummy user_id = 1;
                $this->session->set_userdata('last_name', $row->last_name);
                $this->session->set_userdata('contact', $row->contact);
                $this->session->set_userdata('gender', $row->gender);
                $this->session->set_userdata('country', $row->country_origin_id);
                $this->session->set_userdata('dob', $row->date_of_birth);
                $this->session->set_userdata('email', $row->email);
            }

            if ($this->input->post('rememberMe')) {
                $cookie = array(
                    'name' => 'rememberMe',
                    'value' => $user_id,
                    'expire' => '86500',
                    'secure' => TRUE
                );
                $this->input->set_cookie($cookie);
            }

            //Logged in redirect to main page
            redirect('manga'); //e.g. manga functions
        }
    }

    function login_with_twitter() {
        //Step 1: Let the user login with their Twitter account
        if (!$this->tweet->logged_in()) {

            $this->tweet->set_callback(site_url('main/auth_twitter'));

            $this->tweet->login();
        } else {
            //Logged in redirect to main page
            redirect('manga');
        }

        //TODO Step 2:Checking
        // If their Twitter account info are already inside our database, redirect to main page
        // If their Twitter account info havent had inside our database, insert it, and redirect to main page
    }

    function auth_twitter() {
        $user = $this->tweet->call('get', 'account/verify_credentials');
        $name = $user->name;
        $uid = $user->id_str;

        $this->session->set_userdata('user_id', 1); //dummy user_id = 1;
        redirect('manga');
    }

    function logout() {
        $this->session->sess_destroy();
        redirect('main');
    }

    function not_log_in() {
        $this->layouts->view('not_log_in');
    }

}

/* End of file main.php */
/* Location: ./application/controllers/main.php */

